package com.examples;

import toman.EngineForCreatingQueriesOnly;
import toman.SingleTOQuery;
import toman.TOMan;
import toman.TOQuery;

import java.sql.SQLException;

/**
 * User: zslajchrt
 * Date: 3.5.11
 * Time: 12:58
 */
public class SecurityContextEngine extends EngineForCreatingQueriesOnly {

    private final Object securityContext;

    public SecurityContextEngine(Object securityContext) {
        this.securityContext = securityContext;
    }

    public <T> boolean canCreateQuery(TOMan toman, Class<T> toClass, Object queryDefinition) {
        return toClass.getAnnotation(SecurityContext.class) != null;
    }

    public <T> TOQuery<T> createQuery(final TOMan toMan, final Class<T> toCls, Object queryDefinition) throws Exception {
        return new SingleTOQuery<T>(toMan, toCls) {
            @Override
            protected Object createSingleRawData() throws SQLException {
                return securityContext;
            }
        };
    }
}
